package mop.gui.task;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.LinkedList;
import javax.swing.JOptionPane;
import mop.controller.TaskController;
import mop.exception.IllegalTerminationException;
import mop.gui.main.MainWindow;

public class RemoveTaskAction implements MouseListener {
    
    /** Task description. */
    private String description;
    
    /**
     * Constructor.
     * 
     * @param   description
     *          Task description.
     */
    public RemoveTaskAction(String description)
    {
        this.description = description;
    }

    public void mouseClicked(MouseEvent e)
    {
        int result = JOptionPane.showConfirmDialog(null, 
                "Are you sure you want to remove this task?", "Are you sure?", JOptionPane.YES_NO_OPTION);
        
        if (result == JOptionPane.YES_OPTION)
        {
            LinkedList<String> parents = null;
            
            try {
                parents = (LinkedList<String>) TaskController.getInstance().tryRemoveTask(this.description);
            } catch (IllegalTerminationException ex) {
                JOptionPane.showMessageDialog(null, "Cannot remove task!", "Error", JOptionPane.ERROR_MESSAGE);
            }
            
            if (parents != null)
            {
                result = JOptionPane.showConfirmDialog(null, 
                    "The following tasks depend on this task:\n\n" + printTasks(parents) + "\nDo you want to remove these too?", 
                    "Dependent tasks detected!", JOptionPane.YES_NO_CANCEL_OPTION);
                
                if (result == JOptionPane.YES_OPTION)
                {
                    try {
                        TaskController.getInstance().forceRemoveTask(description, true);
                    } catch (IllegalTerminationException ex) {
                        JOptionPane.showMessageDialog(null, "Cannot remove task!", "Error", JOptionPane.ERROR_MESSAGE);
                    }
                }
                else if (result == JOptionPane.NO_OPTION)
                {
                    try {
                        TaskController.getInstance().forceRemoveTask(description, false);
                    } catch (IllegalTerminationException ex) {
                        JOptionPane.showMessageDialog(null, "Cannot remove task!", "Error", JOptionPane.ERROR_MESSAGE);
                    }
                }
            }
            else
            {
                JOptionPane.showMessageDialog(null, "Task removed!", "Success", JOptionPane.INFORMATION_MESSAGE);
            }    
            
            MainWindow.getInstance().update();
        }
    }
    
    private String printTasks(LinkedList<String> tasks)
    {
        StringBuffer sb = new StringBuffer();
        for(String t : tasks)
            sb.append(t + "\n");
        return sb.toString();
    }

    public void mousePressed(MouseEvent e)
    {
    }

    public void mouseReleased(MouseEvent e)
    {
    }

    public void mouseEntered(MouseEvent e)
    {        
    }

    public void mouseExited(MouseEvent e)
    {
    }

}